﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Threading;
using System.Drawing;

namespace FinallyVideoCaptureTest.Assist
{
    public class ThreadBase
    {
        private SynchronizationContext _synchronizationContext;

        public ThreadBase()
        {
            this._synchronizationContext = AsyncOperationManager.SynchronizationContext;
        }

        public void Post<T>(Action<T> action, T e)
            where T : EventArgs
        {
            if (this._synchronizationContext == null)
            {
                ThreadPool.QueueUserWorkItem(delegate { action(e); });
            }
            else
            {
                this._synchronizationContext.Post(delegate { action(e); }, null);
            }
        }

        public void Send<T>(Action<T> action, T e)
           where T : EventArgs
        {
            if (this._synchronizationContext == null)
            {
                action(e);
            }
            else
            {
                this._synchronizationContext.Send(delegate { action(e); }, null);
            }
        }
    }
}
